﻿using WPay.Infrastructure.Unity.Enums;
using Microsoft.AspNetCore.Http;
using MySql.Data.MySqlClient;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using WPay.Infrastructure.Unity;

namespace WPay.Infrastructure.Repository.DapperCore
{
    public static class DapperFactory
    {
        //用于获取请求上下文的实例       
        private static IHttpContextAccessor accessor = new HttpContextAccessor();
        /// <summary>
        /// 创建数据库连接
        /// </summary>
        /// <param name="_readOnly">是否只读操作[true:是；false:否（默认）]</param>
        /// <returns></returns>
        public static IDbConnection CreateConnection(bool _readOnly = false)
        {
            ////获取上下文中的企业代码，未传直接使用公司测试企业代码 0001
            //string _formCpnid = accessor.HttpContext.Request.HasFormContentType ? accessor.HttpContext.Request.Form["CpnID"].ToString() : null;
            //string _queryCpnid = (accessor.HttpContext.Request.Query != null && !string.IsNullOrEmpty(accessor.HttpContext.Request.Query["CpnID"])) ? accessor.HttpContext.Request.Query["CpnID"].ToString() : "0001";

            //string _cpnID = _formCpnid ?? _queryCpnid;

            //if (!string.IsNullOrWhiteSpace(_cpnID))
            //{
            //    string _connection;
            //    #region 这里为了方便调试直接写的字符串，实例项目中将连接字符串放在Redis中，从Redis中进行连接字符串读取

            //    switch (_cpnID)
            //    {
            //        case "0025":
            //            if (_readOnly)
            //                _connection = @"Data Source=wuaio-cluster.cluster-crdgwrchybgr.rds.cn-northwest-1.amazonaws.com.cn;port=3306;Initial Catalog=wechat;user id=wechat_user;password=F3b6>9Kf;";
            //            else
            //                _connection = @"Data Source=wuaio-cluster.cluster-crdgwrchybgr.rds.cn-northwest-1.amazonaws.com.cn;port=3306;Initial Catalog=wechat;user id=wechat_user;password=F3b6>9Kf;";
            //            break;
            //        default:
            //            _connection = @"Data Source=wuaio-cluster.cluster-crdgwrchybgr.rds.cn-northwest-1.amazonaws.com.cn;port=3306;Initial Catalog=wechat;user id=wechat_user;password=F3b6>9Kf;";
            //            break;
            //    }

            //    #endregion

            //    return new MySqlConnection(_connection);
            //}
            //return null;


            var _connection = ConfigManage.GetConfigByKey("DBConstr");
            return new MySqlConnection(_connection);

        }
    }
}
